/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.swta.components.category;

import de.swta.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 *
 * @author Jan Korreck
 */
public class Categories extends WebPage {

    CategoryForm catForm = new CategoryForm("catForm") {

        @Override
        public void submitClicked() {
            Categories.this.catForm.setEdit(false);
            Categories.this.catForm.setCatName("");
        }
    };
    

    public Categories() {
        
        add(new StyleSheetReference("stylesheet", Categories.class, "..//..//style.css"));

        catForm.add(new FeedbackPanel("feedback"));
        add(catForm);

        List<IColumn> columns = new ArrayList<IColumn>();

        columns.add(new AbstractColumn(new Model("Actions")) {

            public void populateItem(Item cellItem, String componentId, IModel model) {
                 cellItem.add(new CategoryActionPanel(componentId, model, Categories.this) {

                    @Override
                    public void linkClicked() {
                        Categories.this.catForm.setEdit(true);
                    }
                });
            }

        });

        columns.add(new PropertyColumn(new Model("Name"), "name", "name"));

        add(new DefaultDataTable("categoryTable", columns, new SortableCategoryProvider(), 10));

        add(new Link("backLink") {

            @Override
            public void onClick() {
                setResponsePage(StartPage.class);
            }
        });
    }

    public void editSelectedItem(Category editCat) {
        this.catForm.setModelObject(editCat);
        this.catForm.setCatName(editCat.getName());
    }


}
